layui.define(['setter', 'helper', 'user'], function (exports) {

    var setter = layui.setter
        , helper = layui.helper
        , user = layui.user
        , redirect = {}

    redirect.auto = function () {
        if (location.pathname === "/login") {
            redirect.login();
        } else {
            redirect.logout();
            redirect.save(location.pathname);
        }
    }


    redirect.save =function (value) {
        layui.data(setter.tableName, {key: 'history', value: value})
    }

    redirect.login = function () {
        var data = layui.data(setter.tableName)
        var history = helper.getValue(data, 'history', null)
        if (!user.isGuest()) {
            if ( history != null && history.value ) {
                location.href = history.value;
            } else {
                location.href = "/";
            }
        }
    }

    redirect.logout = function () {
        if (user.isGuest()) {
            location.href = "/login";
        }
    }

    exports('redirect', redirect)
})
